import * as crypto from "crypto";
import * as fs from "fs";

function getMD5CB(filePath:string,cb:(e?:Error,value?:string)=>void)
{
    let MD5 = "";
    let stream = fs.createReadStream(filePath);
    let fsHash = crypto.createHash('md5');
    stream.on('error',(e)=>{
        console.error(e.message);
        cb(e);
    });
    stream.on('data', function (d)
    {
        fsHash.update(d);
    });
    stream.on('end', function ()
    {
        MD5 = fsHash.digest('hex');
        cb(undefined,MD5);
    });
}
function getMD5Async(filePath:string):Promise<string>
{
    return new Promise((resolve, reject) => {
        getMD5CB(filePath,(e, value)=>{
            if(e) reject(e);
            else resolve(value);
        })
    });
}

export {getMD5CB,getMD5Async}